<?php
namespace Crm\Table;

class MonitorReportTable extends AbstractTable
{

    protected $table = 'hy_monitor_report';

    protected $id = 'monitor_report_id';

    public function updateItem($id, $data)
    {
        if (empty($id)) {
            throw new \Exception("id field is invalid!");
        }
        $data = array(
            'monitor_report_id' => $data['monitor_report_id'],
            'material_id' => $data['material_id'],
            'image' => $data['image'],
            'reprot_time' => new date()
        )
        ;
        return $this->update($data, array(
            'monitor_report_id' => $id
        ));
    }

    public function deleteItem($id)
    {
        if (empty($id)) {
            throw new Exception("id field is invalid!");
        }
        return $this->delete(array(
            'monitor_report_id' => $id
        ));
    }

    public function addItem($data)
    {
        $data = array(
            'monitor_report_id' => $data['monitor_report_id'],
            'material_id' => $data['material_id'],
            'image' => $data['image'],
            'reprot_time' => $data['reprot_time'],
            'create_time' => new date()
        );
        
        $this->insert($data);
        $id = $this->lastInsertValue;
        return $id;
    }

    public function getItem($id)
    {
        if (empty($id)) {
            throw new Exception("id field is invalid!");
        }
        $sql = 'SELECT * FROM `hy_material` WHERE `monitor_report_id` =:monitor_report_id';
        $statement = $this->getAdapter()->query($sql);
        $results = $statement->execute(array(
            ':monitor_report_id' => $id
        ));
        $row = $results->current();
        return $row;
    }
}